<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: AggregatorDAO.cfc 576 2006-05-16 19:34:28Z mpwoodward $

Notes:
--->
<cfcomponent
	displayname="AggregatorDAO"
	output="false" 
	extends="AggregatorDAO" 
	hint="AggregatorDAO for MachBlog (MSSQL)">

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="AggregatorDAO" 
		hint="Constructor for this CFC.">
		<cfreturn this />
	</cffunction>

	<!---
	PUBLIC FUNCTIONS - CRUD
	--->
	<cffunction name="create" access="public" output="false" returntype="void" hint="Creates a new aggregator">
		<cfargument name="aggregator" type="Aggregator" required="true" />
		
		<cfset var insertAggregator = "" />
		
		<cfquery name="insertAggregator" datasource="#getDatasource().getDsn()#" 
				username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
			INSERT INTO machblog_aggregator (
				aggregator_id, 
				aggregator_name, 
				aggregator_description, 
				aggregator_url, 
				ping_url, 
				auto_ping, 
				created_by_id, 
				dt_created, 
				ip_created, 
				is_active
			) VALUES (
				<cfqueryparam value="#arguments.aggregator.getAggregatorId()#" cfsqltype="cf_sql_char" 
						maxlength="35" />, 
				<cfqueryparam value="#arguments.aggregator.getAggregatorName()#" cfsqltype="cf_sql_varchar" 
						maxlength="100" />, 
				<cfif arguments.aggregator.getAggregatorDescription() NEQ "">
					<cfqueryparam value="#arguments.aggregator.getAggregatorDescription()#" cfsqltype="cf_sql_varchar" 
							maxlength="500" />, 
				<cfelse>
					null, 
				</cfif>
				<cfqueryparam value="#arguments.aggregator.getAggregatorUrl()#" cfsqltype="cf_sql_varchar" 
						maxlength="100" />, 
				<cfqueryparam value="#arguments.aggregator.getPingUrl()#" cfsqltype="cf_sql_varchar" 
						maxlength="250" />, 
				<cfqueryparam value="#arguments.aggregator.getAutoPing()#" cfsqltype="cf_sql_bit" />, 
				<cfqueryparam value="#arguments.aggregator.getAudit().getCreatedById()#" cfsqltype="cf_sql_char" 
						maxlength="35" />, 
				<cfqueryparam value="#arguments.aggregator.getAudit().getDtCreated()#" cfsqltype="cf_sql_bigint" />, 
				<cfqueryparam value="#arguments.aggregator.getAudit().getIpCreated()#" cfsqltype="cf_sql_varchar" 
						maxlength="15" />, 
				<cfqueryparam value="#arguments.aggregator.getAudit().getIsActive()#" cfsqltype="cf_sql_bit" />
			)
		</cfquery>
	</cffunction>
	
	<cffunction name="read" access="public" output="false" returntype="query" hint="Populates a Aggregator bean">
		<cfargument name="aggregatorId" type="string" required="true" />
		
		<cfset var getAggregator = "" />

		<cfquery name="getAggregator" datasource="#getDatasource().getDsn()#" 
				username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
			SELECT 	machblog_aggregator.*, 
					cb.first_name AS cbfn, 
					cb.last_name AS cbln, 
					mb.first_name AS mbfn, 
					mb.last_name AS mbln 
			FROM 	machblog_aggregator 
			LEFT OUTER JOIN machblog_user AS cb 
				ON machblog_aggregator.created_by_id = cb.user_id 
			LEFT OUTER JOIN machblog_user AS mb 
				ON machblog_aggregator.modified_by_id = mb.user_id 
			WHERE machblog_aggregator.aggregator_id = <cfqueryparam value="#arguments.aggregatorId#" cfsqltype="cf_sql_char" />
			ORDER BY aggregator_name ASC
		</cfquery>

		<cfreturn getAggregator />
	</cffunction>
	
	<cffunction name="update" access="public" output="false" returntype="void" 
			hint="Updates an aggregator.">
		<cfargument name="aggregator" type="Aggregator" required="true" />
		
		<cfset var updateAggregator = "" />
		
		<cfquery name="updateAggregator" datasource="#getDatasource().getDsn()#" 
				username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
			UPDATE 	machblog_aggregator 
			SET 	aggregator_name = <cfqueryparam value="#arguments.aggregator.getAggregatorName()#" 
													cfsqltype="cf_sql_char" maxlength="100" />, 
			<cfif arguments.aggregator.getAggregatorDescription() NEQ "">
					aggregator_description = <cfqueryparam value="#arguments.aggregator.getAggregatorDescription()#" 
															cfsqltype="cf_sql_varchar" maxlength="500" />, 
				<cfelse>
					aggregator_description = null, 
			</cfif>
					aggregator_url = <cfqueryparam value="#arguments.aggregator.getAggregatorUrl()#" 
												cfsqltype="cf_sql_varchar" maxlength="100" />, 
					ping_url = <cfqueryparam value="#arguments.aggregator.getPingUrl()#" 
										cfsqltype="cf_sql_varchar" maxlength="250" />, 
					auto_ping = <cfqueryparam value="#arguments.aggregator.getAutoPing()#" cfsqltype="cf_sql_bit" />, 
					modified_by_id = <cfqueryparam value="#arguments.aggregator.getAudit().getModifiedById()#" 
													cfsqltype="cf_sql_char" maxlength="35" />, 
					dt_modified = <cfqueryparam value="#arguments.aggregator.getAudit().getDtModified()#" 
												cfsqltype="cf_sql_bigint" />, 
					ip_modified = <cfqueryparam value="#arguments.aggregator.getAudit().getIpModified()#" 
												cfsqltype="cf_sql_varchar" maxlength="15" />, 
					is_active = <cfqueryparam value="#arguments.aggregator.getAudit().getIsActive()#" 
												cfsqltype="cf_sql_bit" /> 
			WHERE 	aggregator_id = <cfqueryparam value="#arguments.aggregator.getAggregatorId()#" 
												cfsqltype="cf_sql_char" maxlength="35" />
		</cfquery>
	</cffunction>
	
	<cffunction name="delete" access="public" output="false" returntype="void" 
			hint="Deletes an aggregator.">
		<cfargument name="aggregator" type="Aggregator" required="true" />
		
		<cfset var deleteAggregator = "" />
		<cfset var result = true />		

		<cfquery name="deleteAggregator" datasource="#getDatasource().getDsn()#" 
				username="#getDatasource().getUserName()#" 
				password="#getDatasource().getPassword()#">
			DELETE FROM machblog_aggregator 
			WHERE aggregator_id = <cfqueryparam value="#arguments.aggregator.getAggregatorId()#" 
												cfsqltype="cf_sql_char" maxlength="35" />
		</cfquery>
	</cffunction>
	
	<cffunction name="updateLastPing" access="public" output="false" returntype="void" 
			hint="Updates an aggregator's last ping date">
		<cfargument name="aggregatorId" type="uuid" required="true" />
		
		<cfset var updateAggregator = "" />
		
		<cftry>
			<cfquery name="updateAggregator" datasource="#getDatasource().getDsn()#" 
					username="#getDatasource().getUserName()#" password="#getDatasource().getPassword()#">
				UPDATE 	machblog_aggregator 
				SET 	dt_last_pinged = <cfqueryparam value="#getResourceBundleFacade().getLocaleUtils().toEpoch(Now())#" 
												cfsqltype="cf_sql_bigint" /> 
				WHERE 	aggregator_id = <cfqueryparam value="#arguments.aggregatorId#" 
												cfsqltype="cf_sql_char" maxlength="35" />
			</cfquery>
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>
	</cffunction>
</cfcomponent>